<template>
	<view class="content">
		<scroll-view scroll-y class="left-aside">
			<view v-for="item in flist" :key="item.id" class="f-item clamp" :class="{active: item.id === currentId}" @click="tabtap(item)">
				{{item.cName}}
			</view>
		</scroll-view>
		<scroll-view scroll-with-animation scroll-y class="right-aside" @scroll="asideScroll" :scroll-top="tabScrollTop">
			<view v-for="item in currenttab.lsChildClass" :key="item.id" class="s-list" :id="'main-'+item.id">
				<text class="s-item">{{item.cName}}</text>
				<view class="t-list">
					<view @click="navToList(titem,item)" class="t-item" v-for="titem in item.lsChildClass" :key="titem.id">
						<image :src="titem.fullImageUrl"></image>
						<text>{{titem.cName}}</text>
					</view>
				</view>
			</view>
		</scroll-view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				sizeCalcState: false,
				tabScrollTop: 0,
				currentId: 1,
				flist: [],
				slist: [],
				tlist: [],
				currenttab:[]
			}
		},
		onLoad(){
			// this.loadData();
			let that = this;
			let token=that.$SysCache.get('token');
			that.$Request.get(
				that.$api.httpApi.cate.cate+"&token="+that.$SysCache.get('token')
				
				).then(res=>{
					if(res.code == 200){
						console.log(res)
						let list =res.data;
						that.flist=res.data
						console.log(list)
						list.forEach((item,index)=>{
							if(index == 0){
								that.currentId = item.id;
								that.currenttab = item
							}
							
						}) 
					}
				})
		},
	
		methods: {
			// async loadData(){
				
				
				
			// 	let list = await this.$api.json('');
			// 	list.forEach(item=>{
			// 		if(!item.pid){
			// 			this.flist.push(item);  //pid为父级id, 没有pid或者pid=0是一级分类
			// 		}else if(!item.picture){
			// 			this.slist.push(item); //没有图的是2级分类
			// 		}else{
			// 			this.tlist.push(item); //3级分类
			// 		}
			// 	}) 
			// },
			//一级分类点击
			tabtap(item){
				if(!this.sizeCalcState){
					this.calcSize();
				}
				
				this.currentId = item.id;
				// let index = this.slist.findIndex(sitem=>sitem.pid === item.id);
				// this.tabScrollTop = this.slist[index].top;
				this.currenttab = item
			},
			//右侧栏滚动
			asideScroll(e){
				if(!this.sizeCalcState){
					this.calcSize();
				}
				let scrollTop = e.detail.scrollTop;
				let tabs = this.slist.filter(item=>item.top <= scrollTop).reverse();
				if(tabs.length > 0){
					this.currentId = tabs[0].pid;
				}
			},
			//计算右侧栏每个tab的高度等信息
			calcSize(){
				let h = 0;
				this.slist.forEach(item=>{
					let view = uni.createSelectorQuery().select("#main-" + item.id);
					view.fields({
						size: true
					}, data => {
						item.top = h;
						h += data.height;
						item.bottom = h;
					}).exec();
				})
				this.sizeCalcState = true;
			},
			navToList(item,items){
				let id = item.id
				let name = item.cName
				this.$SysCache.put("cateData",items.lsChildClass,864000)
				uni.navigateTo({
					url: `/pages/cate/cateList?categoryid=${id}&name=${name}`
				})
			}
			
		},
		onNavigationBarButtonTap(e) {
			
		  uni.navigateTo({
		  	url: `/pages/cate/indexHeng`
		  })
		}
	}
</script>

<style lang='scss'>
	page,
	
	.content {
		height: 100%;
		background-color: #EEEEEE;
	}

	.content {
		display: flex;
	}
	.left-aside {
		flex-shrink: 0;
		width: 200upx;
		height: 100%;
		background-color: #fff;
	}
	.f-item {
		/* display: flex; */
		align-items: center;
		justify-content: center;
		width: 100%;
		height: 100upx;
		font-size: 28upx;
		color: $font-color-base;
		position: relative;
		line-height: 100upx;
		text-align: center;
		padding: 0 20upx;
		&.active{
			color: #13C0A4;
			background: #EEEEEE;
			&:before{
				content: '';
				position: absolute;
				left: 0;
				top: 50%;
				transform: translateY(-50%);
				height: 36upx;
				width: 4upx;
				background-color: #13C0A4;
				opacity: .8;
			}
		}
	}

	.right-aside{
		flex: 1;
		overflow: hidden;
		padding-left: 20upx;
	}
	.s-item{
		display: flex;
		align-items: center;
		height: 70upx;
		padding: 50upx 0;
		font-size: 28upx;
		color: $font-color-dark;
	}
	.t-list{
		display: flex;
		flex-wrap: wrap;
		width: 97%;
		background: #fff;
		padding-top: 12upx;
		border-radius: 10upx;
		&:after{
			content: '';
			flex: 99;
			height: 0;
		}
		text{
			font-size: 24upx;
			color: #666;
		}
	}
	.t-item{
		flex-shrink: 0;
		display: flex;
		justify-content: center;
		align-items: center;
		flex-direction: column;
		width: 33.33%;
		font-size: 26upx;
		color: #666;
		padding-bottom: 20upx;
		
		image{
			width: 140upx;
			height: 140upx;
		}
	}
</style>
